package com.zt.algorithm.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: TwoSum
 * @Description: TwoSum
 * @Author: tao.zhang1
 * @Date: 2019/11/25 15:48
 * @Version: V1.0
 **/
public class TwoSum {
    public static void main(String[] args) {
        int[] nums = new int[]{1,6,2,7};
        int target = 9;
        int[] indexs = twoSum(nums,target);
        System.out.println(indexs[0] + " " + indexs[1]);
    }
    public static int[] twoSum(int[] nums, int target) {
        int[] indexs = new int[2];
        Map<Integer,Integer> map = new HashMap<>();
        for (int i=0;i<nums.length;i++) {
            int temp = target-nums[i];
            if(map.containsKey(temp)){
                indexs[0] = i;
                indexs[1] = map.get(temp);
                break;
            }
            map.put(nums[i],i);
        }
        return indexs;
    }
}
